<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="workEffortName">
        <el-input v-model="queryParams.workEffortName" placeholder="请输入任务名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="currentStatusId">
        <el-select v-model="queryParams.currentStatusId" placeholder="请选择状态" clearable>
          <el-option v-for="dict in currentStatusIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ehr:task:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleAssign"
          v-hasPermi="['ehr:task:edit']">任务指派</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:task:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="workEffortId" /> -->
      <!-- <el-table-column label="计划类型" align="center" prop="workEffortTypeId" /> -->
      <el-table-column label="任务名称" align="center" prop="workEffortName">
        <template slot-scope="scope">
          <router-link :to="'/workeffort/taskPanel/index/' + scope.row.workEffortId" class="link-type">
            <span>{{ scope.row.workEffortName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="sequenceNum" width="50"/>
      <el-table-column label="项目阶段" align="center" prop="workEffortParentId" />
      <el-table-column label="项目名称" align="center" prop="workEffortName" />
      <!-- <el-table-column label="工作计划目标类型" align="center" prop="workEffortPurposeTypeId" /> -->
      <!-- <el-table-column label="描述" align="center" prop="description" /> -->
      <!-- <el-table-column label="范围" align="center" prop="scopeEnumId" /> -->
      <!-- <el-table-column label="优先权" align="center" prop="priority" width="60"/> -->
      <el-table-column label="预计开始时间" align="center" prop="estimatedStartDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estimatedStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计完成时间" align="center" prop="estimatedCompletionDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estimatedCompletionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="currentStatusId" width="80" :formatter="currentStatusIdFormat" />
      <!-- <el-table-column label="实际开始时间" align="center" prop="actualStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualStartDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际完成时间" align="center" prop="actualCompletionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualCompletionDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="计划工时" align="center" prop="estimatedMilliSeconds" />
      <el-table-column label="实际工时" align="center" prop="actualMilliSeconds" />
      <el-table-column label="创建日期" align="center" prop="createdStamp" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdStamp, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:task:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:task:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="workEffortName">
              <el-input v-model="form.workEffortName" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目阶段" prop="workEffortParentId">
              <el-input v-model="form.workEffortParentId" placeholder="请输入项目阶段" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务序号" prop="sequenceNum">
              <el-input v-model="form.sequenceNum" placeholder="请输入任务序号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workEffortTypeId">
              <el-input v-model="form.workEffortTypeId" placeholder="请输入计划类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优先级" prop="priority">
              <el-input v-model="form.priority" placeholder="请输入优先级" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预计开始时间" prop="estimatedStartDate">
              <el-date-picker clearable v-model="form.estimatedStartDate" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" placeholder="请选择预计开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计完成时间" prop="estimatedCompletionDate">
              <el-date-picker clearable v-model="form.estimatedCompletionDate" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" placeholder="请选择预计完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="状态" prop="currentStatusId">
            <el-select v-model="form.currentStatusId" placeholder="请选择状态">
              <el-option v-for="dict in currentStatusIdOptions" :key="dict.enumCode" :label="dict.description"
                :value="dict.enumCode"></el-option>
            </el-select>
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/ehr/task";
// getProjectPhase
import { lgetProjectPhase } from "@/api/ehr/phase";
export default {
  name: "Task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 计划类型业务字典
      workEffortTypeIdOptions: [],
      // 状态业务字典
      currentStatusIdOptions: [],
      // 项目阶段字典
      phaseOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        workEffortTypeId: null,
        currentStatusId: null,
        workEffortName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workEffortTypeId: [
          { required: true, message: "计划类型不能为空", trigger: "blur" }
        ],
        workEffortName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // 计划类型业务字典
    this.getEnumDicts("WORK_EFFORT_TYPE").then((response) => {
      this.workEffortTypeIdOptions = response.data;
    });
    // 状态业务字典
    this.getEnumDicts("WORK_EFFORT_STATUS").then((response) => {
      this.currentStatusIdOptions = response.data;
    });
    this.getList();
  },
  methods: {
    workEffortTypeIdFormat(row, column) {
      let val = null;
      this.workEffortTypeIdOptions.forEach((item, index) => {
        if (row.workEffortTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    currentStatusIdFormat(row, column) {
      let val = null;
      this.currentStatusIdOptions.forEach((item, index) => {
        if (row.currentStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      this.queryParams.workEffortTypeId = "task";
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workEffortId: null,
        workEffortTypeId: null,
        currentStatusId: null,
        lastStatusUpdate: null,
        workEffortPurposeTypeId: null,
        workEffortParentId: null,
        scopeEnumId: null,
        priority: null,
        percentComplete: null,
        workEffortName: null,
        showAsEnumId: null,
        sendNotificationEmail: null,
        description: null,
        locationDesc: null,
        estimatedStartDate: null,
        estimatedCompletionDate: null,
        actualStartDate: null,
        actualCompletionDate: null,
        estimatedMilliSeconds: null,
        estimatedSetupMillis: null,
        estimateCalcMethod: null,
        actualMilliSeconds: null,
        actualSetupMillis: null,
        totalMilliSecondsAllowed: null,
        totalMoneyAllowed: null,
        moneyUomId: null,
        specialTerms: null,
        timeTransparency: null,
        universalId: null,
        sourceReferenceId: null,
        fixedAssetId: null,
        facilityId: null,
        infoUrl: null,
        recurrenceInfoId: null,
        tempExprId: null,
        runtimeDataId: null,
        noteId: null,
        serviceLoaderName: null,
        quantityToProduce: null,
        quantityProduced: null,
        quantityRejected: null,
        reservPersons: null,
        reserv2ndPPPerc: null,
        reservNthPPPerc: null,
        accommodationMapId: null,
        accommodationSpotId: null,
        revisionNumber: null,
        createdDate: null,
        createdByUserLogin: null,
        lastModifiedDate: null,
        lastModifiedByUserLogin: null,
        sequenceNum: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workEffortId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加任务";
    },
    handleAssign(row) {
      const workEffortId = row.workEffortId || this.ids[0]
      // console.info("handleAssign query : " + workEffortId)
      var _path = "/task/assign/";
      this.$router.push({
        path: _path,
        query: {
          taskId: workEffortId,
        },
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const workEffortId = row.workEffortId || this.ids
      getTask(workEffortId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "任务详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const workEffortId = row.workEffortId || this.ids
      getTask(workEffortId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.workEffortTypeId = "task";
          if (this.form.workEffortId != null && this.updateFlag) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workEffortIds = row.workEffortId || this.ids;
      this.$modal.confirm('是否确认删除任务编号为"' + workEffortIds + '"的数据项？').then(function () {
        return delTask(workEffortIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
